<template>
    <div class="manage-org">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="code">
                    <el-input v-model="searchForm.code"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listOrg">Search</el-button>
                    <el-button type="primary" @click="listOrgTree">Refresh</el-button>
                    <el-button type="primary" @click="handleCreate('0')">New</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                @row-dblclick="handleRowDblClick"
                border
                default-expand-all
                highlight-current-row
                :data="orgTable"
                row-key="ukId"
                height="500"
                size="mini"
                style="width: 100%"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="name"
                    label="name"
                    width="*">
            </el-table-column>
            <el-table-column
                    prop="code"
                    label="code"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="status"
                    width="70">
                <template slot-scope="scope">
                    <el-switch
                            @change="statusChange($event, scope.row.ukId)"
                            :value="scope.row.status != '1'"
                            active-color="#909399"
                            inactive-color="#67c23a">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    prop="sort"
                    label="sort"
                    width="60">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="235">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleCreate(scope.row.ukId)">新建下级
                    </el-button>
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row.ukId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-drawer
                title="机构信息"
                :visible.sync="showOrgDetails"
                :with-header="false">
            <table>
                <tr>
                    <td>name:</td>
                    <td>{{this.orgForm.name}}</td>
                </tr>
                <tr>
                    <td>code:</td>
                    <td>{{this.orgForm.code}}</td>
                </tr>
                <tr>
                    <td>status:</td>
                    <td>{{this.orgForm.status}}</td>
                </tr>
                <tr>
                    <td>sort:</td>
                    <td>{{this.orgForm.sort}}</td>
                </tr>
                <tr>
                    <td>gmtCreate:</td>
                    <td>{{this.orgForm.gmtCreate}}</td>
                </tr>
                <tr>
                    <td>gmtModified:</td>
                    <td>{{this.orgForm.gmtModified}}</td>
                </tr>
                <tr>
                    <td>abbreviation:</td>
                    <td>{{this.orgForm.orgInfo.abbreviation}}</td>
                </tr>
                <tr>
                    <td>functional:</td>
                    <td>{{this.orgForm.orgInfo.functional}}</td>
                </tr>
                <tr>
                    <td>leader:</td>
                    <td>{{this.orgForm.orgInfo.leader}}</td>
                </tr>
                <tr>
                    <td>level:</td>
                    <td>{{this.orgForm.orgInfo.level}}</td>
                </tr>
                <tr>
                    <td>telephone:</td>
                    <td>{{this.orgForm.orgInfo.telephone}}</td>
                </tr>
                <tr>
                    <td>gmtCreate:</td>
                    <td>{{this.orgForm.orgInfo.gmtCreate}}</td>
                </tr>
                <tr>
                    <td>gmtModified:</td>
                    <td>{{this.orgForm.orgInfo.gmtModified}}</td>
                </tr>
            </table>
        </el-drawer>
        <el-dialog title="机构管理" :visible.sync="showOrgForm" :close-on-click-modal="false" label-width="80px">
            <el-row>
                <el-col :span="12">
                    <el-form ref="form" :model="orgForm" label-width="80px">
                        <el-form-item label="机构编码">
                            <el-input v-model="orgForm.code"></el-input>
                        </el-form-item>
                        <el-form-item label="机构名称">
                            <el-input v-model="orgForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-select v-model="orgForm.status" placeholder="请选择状态">
                                <el-option
                                        v-for="item in this.$store.state.Pabu.dict.manage.STATUS.dictItems"
                                        :key="item.ukId"
                                        :label="item.name"
                                        :value="parseInt(item.value)">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-input v-model="orgForm.sort" type="number" min="0" max="90000"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleSave">保存</el-button>
                            <el-button @click="showOrgForm = false">取 消</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="12">
                    <el-form ref="form" :model="orgForm" label-width="80px">
                        <el-form-item label="简称">
                            <el-input v-model="orgForm.orgInfo.abbreviation"></el-input>
                        </el-form-item>
                        <el-form-item label="职能">
                            <el-input v-model="orgForm.orgInfo.functional"></el-input>
                        </el-form-item>
                        <el-form-item label="领导">
                            <el-input v-model="orgForm.orgInfo.leader"></el-input>
                        </el-form-item>
                        <el-form-item label="级别">
                            <el-input v-model="orgForm.orgInfo.level"></el-input>
                        </el-form-item>
                        <el-form-item label="电话">
                            <el-input v-model="orgForm.orgInfo.telephone"></el-input>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
        </el-dialog>
    </div>
</template>

<script>
  import {deleteOrg, listOrg, listOrgTree, saveOrg, updateOrgStatus} from "../../../api/manage/org";

  export default {
    name: "Org",
    data () {
      return {
        showOrgForm: false,
        showOrgDetails: false,
        orgTable: [],
        searchForm: {},
        orgForm: {
          orgInfo: {}
        },
      }
    },
    methods: {
      handleCreate (parentId) {
        this.orgForm = {
          parentId: parentId,
          status: 1,
          orgInfo: {}
        }
        this.showOrgForm = true
      },
      handleEdit (row) {
        this.orgForm = row
        this.showOrgForm = true
      },
      handleSave () {
        saveOrg(this.orgForm).then(result => {
          this.listOrgTree()
          this.showOrgForm = false
        }).catch(error => {
          console.log(error)
        })
      },
      handleDelete (ukId) {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteOrg(ukId).then(result => {
            this.listOrgTree()
            this.showOrgForm = false
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      statusChange (value, orgId) {
        let status = value ? 0 : 1
        updateOrgStatus(orgId, status).then(result => {
          this.listOrgTree()
        }).catch(error => {
          console.log(error)
        })
      },
      handleRowDblClick (row) {
        this.orgForm = row
        this.showOrgDetails = true
      },
      listOrg () {
        listOrg(this.searchForm).then(result => {
          this.orgTable = result.data.orgs.records
        }).catch(error => {
          console.log(error)
        })
      },
      listOrgTree () {
        listOrgTree("0").then(result => {
          this.orgTable = result.data.orgTree
        }).catch(error => {
          console.log(error)
        })
      }
    },
    created () {
      this.listOrgTree()
    }
  }
</script>

<style scoped>
    .manage-org {
        width: 100%;
        height: 100%;
    }

    /* element ui样式重写 */
    .manage-org > .el-table {
        height: calc(100% - 35px) !important;
    }

    .manage-org > .el-table > .el-table__body-wrapper {
        height: calc(100% - 35px) !important;
    }
</style>
